const connect = require('./base');
function course() {
    this.queryCourse = (req,callback) => {
        let sql = null;
        if (req.query.id) {
            sql = 'select * from course where id=' + req.query.id
        } else {
            sql = 'select * from course';
        }
        connect.getConnection((err, connection) => {
            if (err) {
                console.log('连接失败');
                return;
            } else {
                connection.query(sql, function (error, row, field) {
                    if (error) {
                        console.log('查询失败' + error.message);
                        return;
                    }
                    connection.release(); //每一次用完都要释放
                    callback(row);
                });
            }
        })
    }
    this.add = (params,callback) => {
        let sql = 'insert into course set ?',data = {
            title: params.title,
            createDate: String(new Date().getTime()),
            content: params.content,
            type: params.type,
            cover:　params.cover
        };
        connect.getConnection((err, connection) => {
            if (err) {
                console.log('连接失败');
                return;
            } else {
                connection.query(sql,data, function (error, row, field) {
                    if (error) {
                        console.log('查询失败' + error.message);
                        return;
                    }
                    connection.release(); //每一次用完都要释放
                    callback(row);
                });
            }
        })
    }
    this.del = (params, callback) => {
        let sql = 'delete from course where id='+params.id;
        connect.getConnection((err, connection) => {
            if (err) {
                console.log('连接失败');
                return;
            } else {
                connection.query(sql, function (error, row, field) {
                    if (error) {
                        console.log('查询失败' + error.message);
                        return;
                    }
                    connection.release(); //每一次用完都要释放
                    callback(row);
                });
            }
        })
    }
    this.delAll = (params,callback) => {
        let arr = params.data.split(',');
        arr.forEach(item => {
            let sql = 'delete from course where id='+item;
            connect.getConnection((err, connection) => {
                if (err) {
                    console.log('连接失败');
                    return;
                } else {
                    connection.query(sql, function (error, row, field) {
                        if (error) {
                            console.log('查询失败' + error.message);
                            return;
                        }
                        connection.release(); //每一次用完都要释放
                        callback(row);
                    });
                }
            })
        })
    }
    this.search = (params, callback) => {
        let sql = 'select * from course where type='+params.type;
        connect.getConnection((err, connection) => {
            if (err) {
                console.log('连接失败');
                return;
            } else {
                connection.query(sql, function (error, row, field) {
                    if (error) {
                        console.log('查询失败' + error.message);
                        return;
                    }
                    connection.release(); //每一次用完都要释放
                    callback(row);
                });
            }
        })
    }
};
module.exports = course;
